package com.sas.web.clients;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.sas.domain.Address;
import com.sas.domain.Client;
import com.sas.domain.Telephone;
import com.sas.repository.ClientRepository;
import com.sas.web.SasBasePage;
import com.sas.web.panel.clients.ClientInfoAddPanel;

public class ClientAddPage extends SasBasePage {
	@SpringBean
	private ClientRepository clientRepo;
	
	transient private String cuilCuit;
	transient private String name;
	transient private String description;
	transient private String email;
	
	//address
	transient private String street;
	transient private Integer number;
	transient private String department;
	transient private String city;
	transient private String province;
	transient private String postalCode;
	transient private String country;
	
	//telephone
	transient private Integer countryCode;
	transient private Integer areaCode;
	transient private Integer telephoneNumber;
	transient private String intern;
	
	//alternative tel
	transient private Integer countryCodeAlternative;
	transient private Integer areaCodeAlternative;
	transient private Integer telephoneNumberAlternative;
	transient private String internAlternative;
	
	//fax
	transient private Integer countryCodeFax;
	transient private Integer areaCodeFax;
	transient private Integer telephoneNumberFax;
	transient private String internFax;
	
	//cellphone
	transient private Integer countryCodeCellphone;
	transient private Integer areaCodeCellphone;
	transient private Integer telephoneNumberCellphone;
	transient private String internCellphone;
	
	public ClientAddPage() {
		Form<Client> form = new Form<Client>("form", new CompoundPropertyModel<Client>(this));
		form.add(new ClientInfoAddPanel("clientInfo"));
		form.add(new Button("save", new ResourceModel("save")) {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 2432453940795257055L;

			@Override
			public void onSubmit() {
				Address address = null;
				address = new Address(street, number, department, city, province, country, postalCode);
				
				Telephone telephone = null;
				if (telephoneNumber != null) {
					telephone = new Telephone(countryCode, areaCode, telephoneNumber, intern);
				}
				
				Telephone alternative = null;
				if (telephoneNumberAlternative != null) {
					alternative = new Telephone(countryCodeAlternative, areaCodeAlternative, telephoneNumberAlternative, internAlternative);
				}
				
				Telephone fax = null;
				if (telephoneNumberFax != null) {
					fax = new Telephone(countryCodeFax, areaCodeFax, telephoneNumberFax, internFax);
				}
				
				Telephone cellphone = null;
				if (telephoneNumberCellphone != null) {
					cellphone = new Telephone(countryCodeCellphone, areaCodeCellphone, telephoneNumberCellphone, internCellphone);
				}
				
				Client client = new Client(cuilCuit, name, description, email, 
						address, telephone, alternative, fax, cellphone);
				
//				client.setCuilCuit(cuilCuit);
//				client.setName(name);
//				client.setDescription(description);
//				client.setEmail(email);
//				client.setAddress(address);
//				client.setTelephone(telephone);
//				client.setAlternative(alternative);
//				client.setFax(fax);
//				client.setCellphone(cellphone);
				
				if (clientRepo.exists(client)) {
					error(getString("clientExists"));
					return;
				}
				
				clientRepo.store(client);
				
				setResponsePage(ClientsPage.class);
				
				
				
			}
		});
		
		FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(form);
	}

}
